#include "epd.h"

#if (EPD_DRIVER_TYPE == EPD_DRIVER_9IN7_BW)
#include "epd_9in7_bw.h"
static epd_driver_t* epd_driver = &epd_9in7_bw;
#endif

void epd_device_init()
{
    if (epd_driver == NULL) {
        return;
    }
    epd_driver->init();
}

void epd_device_refresh(epd_refresh_t refresh)
{
    if (epd_driver == NULL) {
        return;
    }
    epd_driver->refresh(refresh);
}

void epd_device_write_image(uint16_t x_start, uint16_t y_start, uint16_t width, uint16_t height, uint8_t *data, bool full_update)
{
    uint32_t size = width * height / 8;
    uint8_t count = full_update ? 2 : 1;
    epd_ram_t ram_type = EPD_CUR_RAM;

    if (epd_driver == NULL || width == 0 || height == 0) {
        return;
    }
    epd_driver->select_window(x_start, y_start, width, &height);

    for (uint8_t k = 0; k < count; k++) {
        ram_type = (k == 0 ? EPD_CUR_RAM : EPD_OLD_RAM);
        epd_driver->select_cursor(x_start, y_start);
        epd_driver->select_ram(ram_type);
        epd_driver->send_data_block(data, size);
    }
}

int8_t epd_device_read_temp(void)
{
    if (epd_driver == NULL) {
        return -128; // todo 错误代码，表示驱动未初始化 
    }
    return epd_driver->read_temp();
}